package cn.itsource.crm.web.controller;

import cn.itsource.AjaxResult;
import cn.itsource.crm.domain.Dict;
import cn.itsource.crm.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/dict")
@Api(tags = "数据字典类")
@CrossOrigin
public class DictController {

    @Autowired
    private IDictService dictService;

    @RequestMapping(value = "/queryAll",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @ApiOperation(value="查询字典所有类型及其明细",notes = "queryAll查询方法")
    @CrossOrigin
    public List<Dict> queryAll(){
        System.out.println("patch请求");
        return dictService.queryAll();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value="根据ID查询类型",notes = "queryOne查询方法")
    @ResponseBody
    public Dict getOne(@PathVariable Long id){
        return  dictService.queryOne(id);
    }

    @RequestMapping(value = "/save",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value="保存字典类型方法",notes = "save方法")
    public AjaxResult save(@RequestBody  Dict dict){
        try {
            System.out.println("接收的dict对象:");
            dictService.save(dict);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");

        }
        return new AjaxResult();
    }
    //删除
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value="根据id删除字典类型及type对应的明细",notes = "delete方法")
    public AjaxResult delete(@PathVariable Long id){
        try {
            System.out.println("删除的id是:"+id);
            dictService.delete(id);

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");

        }
        return new AjaxResult();
    }

    //修改
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改字典类型方法",notes = "update方法")
    public AjaxResult update(@RequestBody  Dict dict){
        try {
            System.out.println("修的对象:"+dict);
            System.out.println(dict.getId());
            dictService.update(dict);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");
        }
        return new AjaxResult();
    }
}
